package lk.service.blogspringboot.service.impl;

import lk.service.blogspringboot.domain.ArticleInfo;
import lk.service.blogspringboot.mapper.ArticleInfoMapper;
import lk.service.blogspringboot.service.ArticleInfoService;
import lk.service.blogspringboot.util.R;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 文章信息表(ArticleInfo)表服务实现类
 *
 * @author LK
 * @since 2024-08-22 10:33:39
 */
@Service("articleInfoService")
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Resource
    private ArticleInfoMapper articleInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param articleId 主键
     * @return 实例对象
     */
    @Override
    public R queryById(Integer articleId) {
        System.out.println("查询文章信息");
        //// 获取用户信息
        //Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //if (authentication!= null && authentication.isAuthenticated()) {
        //    Object principal = authentication.getPrincipal();
        //    if (principal instanceof UserDetails) {
        //        UserDetails userDetails = (UserDetails) principal;
        //        String username = userDetails.getUsername();
        //        System.out.println("当前用户名：----------------" + username);
        //        // 可以获取其他用户属性
        //    }
        //}
        return R.ok().setData(this.articleInfoMapper.queryById(articleId));
    }

    /**
     * 全查询
     *
     * @param articleInfo 筛选条件
     * @return 查询结果
     */
    @Override
    public R queryAll(ArticleInfo articleInfo) {
        return R.ok().setData(this.articleInfoMapper.queryAll(articleInfo));
    }

    /**
     * 新增数据
     *
     * @param articleInfo 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(ArticleInfo articleInfo) {
        this.articleInfoMapper.insert(articleInfo);
        return R.ok().setData(articleInfo);
    }

    /**
     * 修改数据
     *
     * @param articleInfo 实例对象
     * @return 实例对象
     */
    @Override
    public R update(ArticleInfo articleInfo) {
        this.articleInfoMapper.update(articleInfo);
        return R.ok().setData(this.queryById(articleInfo.getArticleId()));
    }

    /**
     * 通过主键删除数据
     *
     * @param articleId 主键
     * @return 是否成功
     */
    @Override
    public R deleteById(Integer articleId) {
        boolean del = this.articleInfoMapper.deleteById(articleId) > 0;
        return R.ok().setData(del);
    }

    @Override
    public R queryHotArticleInfo() {
        return R.ok().setData(this.articleInfoMapper.queryHotArticleInfo());
    }

    @Override
    public R queryLatestArticleInfo() {
        return R.ok().setData(this.articleInfoMapper.queryLatestArticleInfo());
    }

    @Override
    public R queryAllPublic(ArticleInfo articleInfo) {
        return R.ok().setData(this.articleInfoMapper.queryAllPublic(articleInfo));
    }

    /**
     * 通过文章id的列表查询文章信息
     * @param articleInfo
     * @return
     */
    @Override
    public R queryByArticleIdList(ArticleInfo articleInfo) {
        return R.ok().setData(this.articleInfoMapper.queryByArticleIdList(articleInfo));
    }
}

